//
//  TestGestureCtr.m
//  iOSDemo
//
//  Created by mokai on 14-10-9.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "TestGestureCtr.h"

@interface TestGestureCtr ()
@property (weak, nonatomic) IBOutlet UILabel *dragView;


@property(nonatomic,strong) UIView *focusView;
@end

@implementation TestGestureCtr

/**
 *  1、轻击手势（TapGestureRecognizer）
 
 点一下显示到最上面
 *
 *  @param sender
 */
- (IBAction)tapHandler:(UITapGestureRecognizer *)sender {
    [sender.view.superview bringSubviewToFront:sender.view];
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    
    CALayer *layer = sender.view.layer;
    layer.masksToBounds = YES;
    layer.borderColor = [UIColor blackColor].CGColor;
    layer.borderWidth = 1;
    
    _focusView.layer.borderWidth = 0;
    _focusView = sender.view;
    
    [UIView commitAnimations];
}
/***
 2、长按手势（LongPressGestureRecognizer）
 
 ***/
- (IBAction)longPressRotate:(UILongPressGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateBegan) {//手势完成
        CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];
        
        //把值调换即可调整旋转方向
        theAnimation.values = [NSArray arrayWithObjects:
                               [NSValue valueWithCATransform3D:CATransform3DMakeRotation(6.26, 0,0,1)],
                               [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13, 0,0,1)],
                               [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,0,1)],
                               nil];
        //theAnimation.cumulative = YES;
        theAnimation.duration = 3;
        theAnimation.repeatCount = 1;
        //theAnimation.removedOnCompletion = YES;
        
        theAnimation.timingFunctions = [NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                        nil
                                        ];
        
        [sender.view.layer addAnimation:theAnimation forKey:@"transform"];
    }
}



/**
 *  拖动
 *
 *  @param recognizer
 */
- (IBAction)dragHandle:(UIPanGestureRecognizer*) recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
    
    NSLog(@"drag");
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        CGPoint velocity = [recognizer velocityInView:self.view];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;
        NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
        
        float slideFactor = 0.1 * slideMult; // Increase for more of a slide
        CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
                                         recognizer.view.center.y + (velocity.y * slideFactor));
        finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
        finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
        
        [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            recognizer.view.center = finalPoint;
        } completion:nil];
        
    }
}

/**
 *  缩放
 *
 *  @param recognizer
 */
- (IBAction)pinchHandle:(UIPinchGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
    NSLog(@"pinch");
}

/**
 *  旋转
 *
 *  @param sender
 */
- (IBAction)rotationHandle:(UIRotationGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0;
    NSLog(@"rotation");
}



@end
